package com.nine.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nine.shop.common.ServerResponse;
import com.nine.shop.mapper.AdminMapper;
import com.nine.shop.model.Admin;
import com.nine.shop.model.User;
import com.nine.shop.utils.MD5Util;
import org.springframework.stereotype.Service;

@Service
public class AdminService  extends ServiceImpl<AdminMapper, Admin> {

    /**
     * 查找用户名
     * @param adminName
     * @return
     */
    private Admin findByAdminName(String adminName) {
        return baseMapper.selectOne(new QueryWrapper<Admin>()
                .eq("adminName", adminName));
    }

    /**
     * 登录
     * @param adminName
     * @param password
     * @return
     */
    public ServerResponse login(String adminName,String password) {

        if (adminName == null ||adminName.equals("")){
            return ServerResponse.createByErrorMsg("用户名为空！");
        }else if (password == null || password.equals("")){
            return ServerResponse.createByErrorMsg("密码为空！");
        }
       password = MD5Util.MD5EncodeUtf8(password);
        Admin ad = findByAdminName(adminName);
        if (ad == null)
            return ServerResponse.createByErrorMsg("用户不存在");
        else if (!ad.getPassword().equals(password))
            return ServerResponse.createByErrorMsg("密码错误");
        else if (ad.getStatus() == 0){
            return ServerResponse.createByErrorMsg("该管理员账户尚未激活");
        }
        else {

            Admin admin = changeAdmin(ad);//密码隐藏
            return ServerResponse.createBySuccess(admin);//登录成功
        }
    }

    /**
     * 修改密码
     * @param id
     * @param oldPassword
     * @param newPassword
     * @return
     */
    public ServerResponse updatePassword (int id,String oldPassword, String newPassword ) {

        Admin admin = getById(id);
        if (admin == null){
            return ServerResponse.createByErrorMsg("未查询到管理员信息，请先登录");
        }
        if (oldPassword == null || oldPassword.equals("")){
            return ServerResponse.createByErrorMsg("密码为空");
        }
        if (newPassword == null || newPassword.equals("")){
            return ServerResponse.createByErrorMsg("新密码不能为空");
        }
        oldPassword = MD5Util.MD5EncodeUtf8(oldPassword);
        newPassword = MD5Util.MD5EncodeUtf8(newPassword);

        if (oldPassword == newPassword){
            return ServerResponse.createByErrorMsg("修改的新密码不能与原密码一样");
        }
        else if (admin.getPassword().equals(oldPassword)){
            admin.setPassword(newPassword);
            updateById(admin);
            Admin ad = changeAdmin(admin);//密码隐藏
            return ServerResponse.createBySuccess(ad);//修改成功
        }else
            return ServerResponse.createByErrorMsg("密码错误");
    }

    /**
     * 密码保密
     * @param admin
     * @return
     */
    public Admin changeAdmin(Admin admin){
        Admin ad = new Admin();
        ad.setId(admin.getId());
        ad.setAdminName(admin.getAdminName());
        ad.setStatus(admin.getStatus());
        return ad;
    }
}
